package cn.chair.controller.admin;


import cn.chair.dao.PackageGroupDao;
import cn.chair.dao.WebPackageDao;
import cn.chair.entity.PackageGroup;
import cn.chair.entity.WebPackage;
import cn.chair.model.admin.PackageGroupReq;
import cn.chair.model.admin.PackageGroupsResp;
import life.innovation.builder.SqlBuilder;
import life.innovation.exception.DataWriteException;
import life.innovation.model.Page;
import life.innovation.util.PagerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value="packageGroup")
@RestController
public class PackageGroupController {

    @Autowired
    @Lazy
    private PackageGroupDao packageGroupDao;

    @Autowired
    @Lazy
    private WebPackageDao webPackageDao;


    /**
     * 获取list价格组
     * @param page
     * @param countPerPage
     * @return
     */
   @GetMapping(value="getPackageList")
   public PackageGroupsResp getPackage(@RequestParam(required = false,defaultValue = "1") int page,
                          @RequestParam(required = false,defaultValue = "10") int countPerPage){

       PackageGroupsResp resp = new PackageGroupsResp();
       List<PackageGroup> list =  packageGroupDao.find(SqlBuilder.init());

       List<PackageGroupsResp.PackageGroupList> pList = new ArrayList<>();

       for(PackageGroup packageGroup:list){
           PackageGroupsResp.PackageGroupList p = new PackageGroupsResp.PackageGroupList();
           p.setGroupId(packageGroup.getId());
           p.setGroupName(packageGroup.getName());
           pList.add(p);
       }

       Page pagination = PagerUtil.process(page, countPerPage, pList.size());

       resp.setPagination(pagination);
       resp.setList(pList);

       return resp;


   }

    /**
     * 查询价格组列表信息
     * @param page
     * @param countPerPage
     * @return
     */
    @GetMapping(value="getPackages")
    public PackageGroupsResp getPackages(@RequestParam(required = false,defaultValue = "1") int page,
                                         @RequestParam(required = false,defaultValue = "20") int countPerPage){

        PackageGroupsResp resp = new PackageGroupsResp();

        List<PackageGroupsResp.PackageGroupList> packageGroupList = new ArrayList<>();

        List<PackageGroup> packageGroups = packageGroupDao.find(SqlBuilder.init());
        if(packageGroups != null&&packageGroups.size() > 0){
            for(PackageGroup packageGroup:packageGroups){
                PackageGroupsResp.PackageGroupList pg = new PackageGroupsResp.PackageGroupList();
                pg.setGroupId(packageGroup.getId());
                pg.setGroupName(packageGroup.getName());
                pg.setDescription(packageGroup.getDescription());

                List<WebPackage> packages = webPackageDao.find(SqlBuilder.init().where("package_group_id",packageGroup.getId()));

               List<PackageGroupsResp.PackageGroupList.PackageList> packageLists = new ArrayList<>();
               if(packages != null&&packages.size()>0){
                   for(WebPackage webPackage:packages){
                       PackageGroupsResp.PackageGroupList.PackageList p = new PackageGroupsResp.PackageGroupList.PackageList();
                       p.setPackageName(webPackage.getName());
                       p.setAmount(webPackage.getAmount());
                       p.setTime(webPackage.getTime());

                       packageLists.add(p);
                   }

               }

               pg.setPackagelist(packageLists);

               packageGroupList.add(pg);
            }
         }
        Page pagination = PagerUtil.process(page, countPerPage, packageGroupList.size());
        resp.setPagination(pagination);
        resp.setList(packageGroupList);

        return resp;
    }


    /**
     * 新增价格组信息
     * @param req
     * @return
     * @throws DataWriteException
     */
    @PostMapping(value="addPackage")
    @Transactional
    public Map<String,Object> addPackage(@RequestBody PackageGroupReq req) throws DataWriteException {
        List<WebPackage> webPackages = req.getPackageList();

        PackageGroup group = new PackageGroup();
        group.setName(req.getGroupName());
        group.setDescription(req.getDescription());
        packageGroupDao.insert(group);

        if(webPackages !=null&&webPackages.size()>0){
            for(WebPackage webPackage:webPackages){
                WebPackage wp = new WebPackage();
                wp.setName(webPackage.getName());
                wp.setTime(webPackage.getTime()+"分钟");
                wp.setAmount(webPackage.getAmount()+"元");
                wp.setStatus(1);
                wp.setPackageGroupId(group.getId());

                webPackageDao.insert(wp);
            }
        }


        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }

    /**
     * 通过groupId 获取单条价格组
     *
     * @param groupId
     */
    @GetMapping(value="{groupId}")
    public PackageGroupReq getPackage(@PathVariable int groupId){
        PackageGroupReq resp = new PackageGroupReq();
        PackageGroup packageGroup = packageGroupDao.findOne(SqlBuilder.init().where("id",groupId));
        if(packageGroup != null){
            resp.setGroupId(packageGroup.getId());
            resp.setGroupName(packageGroup.getName());
            resp.setDescription(packageGroup.getDescription());

            List<WebPackage> webPackages = webPackageDao.find(SqlBuilder.init().where("package_group_id",groupId));
            if(webPackages != null&&webPackages.size()>0){
                for(WebPackage webPackage:webPackages){
                    webPackage.setAmount(webPackage.getAmount().replace("元",""));
                    webPackage.setTime(webPackage.getTime().replace("分钟",""));
                }

                resp.setPackageList(webPackages);
            }

        }

        return resp;
    }

    /**
     * 更新单个价格组
     *
     * @param req
     * @return
     * @throws DataWriteException
     */
    @PutMapping(value="updatePackage")
    @Transactional
    public Map<String,Object> updatePackage(@RequestBody PackageGroupReq req) throws DataWriteException {
        PackageGroup packageGroup = packageGroupDao.findOne(SqlBuilder.init().where("id",req.getGroupId()));
        if(packageGroup !=null){
            packageGroup.setName(req.getGroupName());
            packageGroup.setDescription(req.getDescription());
            packageGroupDao.update(packageGroup);

            List<WebPackage> webPackages = req.getPackageList();
            if(webPackages != null&&webPackages.size()>0){
                for(WebPackage webPackage:webPackages){
                    WebPackage wp = webPackageDao.findOne(SqlBuilder.init().where("id",webPackage.getId()));
                    if(wp != null){
                        wp.setName(webPackage.getName());
                        wp.setTime(webPackage.getTime()+"分钟");
                        wp.setAmount(webPackage.getAmount()+"元");

                        webPackageDao.update(wp);
                    }
                }
            }

        }
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }

}
